package com.stylefeng.guns.modular.system.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.DbqMapper;
import com.stylefeng.guns.modular.system.model.Dbq;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URL;

@Service
public class DbqImportTask {

    private Logger log = LoggerFactory.getLogger(DbqImportTask.class);

    private static final HttpClientUtil clientUtil = new HttpClientUtil();

    @Autowired
    private DbqMapper dbqMapper;

    public void execute() {
        int maxId = dbqMapper.selectMaxId();
        String url = ImportContants.getDbqPathStr(maxId +1);
        HttpGet httpGet = HttpClientUtil.getGetRequest();
        try {
            httpGet.setURI(new URL(url).toURI());
            String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6, 10001);
            JSONObject jsonObj = JSON.parseObject(json);
            if (jsonObj.getString("msg").equals("succ")) {
                String name = jsonObj.getJSONObject("data").getString("name");
                Integer id = jsonObj.getJSONObject("data").getInteger("id");
                String time = jsonObj.getJSONObject("data").getString("time");
                String content = jsonObj.getJSONObject("data").getJSONObject("content").getString("text");

                if(StringUtils.contains(content,"交易计划")){
                    if(jsonObj.getJSONObject("data").containsKey("img_list")){
                        JSONObject imgUrl = (JSONObject) jsonObj.getJSONObject("data").getJSONArray("img_list").get(0);
                        content += StringUtils.replace(imgUrl.getString("original"),"\\/","/");
                    }
                }
                if(StringUtils.contains(content,"热点复盘")){
                    if(jsonObj.getJSONObject("data").containsKey("img_list")){
                        JSONObject imgUrl = (JSONObject) jsonObj.getJSONObject("data").getJSONArray("img_list").get(0);
                        content += StringUtils.replace(imgUrl.getString("original"),"\\/","/");
                    }
                }

                if(StringUtils.contains(content,"机会分享")){
                    if(jsonObj.getJSONObject("data").containsKey("list")){
                        JSONArray jSONArray = jsonObj.getJSONObject("data").getJSONArray("list");
                        for(int i=0;i<jSONArray.size();i++){
                            content += ((JSONObject)jSONArray.get(i)).getJSONObject("content").getString("text");
                        }
                    }
                    if(jsonObj.getJSONObject("data").containsKey("img_list")){
                        JSONObject imgUrl = (JSONObject) jsonObj.getJSONObject("data").getJSONArray("img_list").get(0);
                        content += StringUtils.replace(imgUrl.getString("original"),"\\/","/");
                    }
                }

                Dbq dbq = new Dbq();
                if(StringUtils.contains(time,"-")){
                    dbq.setgTime(DateUtil.parseTime("2018-" + time + ":00"));
                }else{
                    dbq.setgTime(DateUtil.parseTime("2018-" + DateUtil.getMM_DDTime() + " "+ time + ":00"));
                }
                dbq.setgId(id);
                dbq.setgName(name);
                dbq.setgContent(content);
                dbqMapper.insert(dbq);
            }
        } catch (Exception e) {
            log.error("get dbq data is error!" + e);
        } finally {
            httpGet.abort();
        }
    }
}
